package sisi.broker;

import java.util.List;

import javax.persistence.EntityManager;

/**
 * BrokerController.java - Controller class for the <i>broker database table</i>. 
 * @author Pedro Gonzalez pedro@sisisoftware.it
 * @author Claudio Pelosi claudio@sisisoftware.it
 * @author SISI Software S.n.c. - www.sisisoftware.it
 * @version 1.0, 21/11/2013
 * 
 */

public class BrokerController {

	private EntityManager getEntityManager() {
		return new sisi.MyEmf().getEntityManager();
	}

	public Broker[] getBroker(String cerca) {
		List<Broker> brokers = getBrokerList(cerca);
		return brokers.toArray(new Broker[0]);
	}

	@SuppressWarnings("unchecked")
	public List<Broker> getBrokerList(String cerca) {

		String cWhere = "";
		if (cerca != null && !cerca.isEmpty()) {
			cWhere = " WHERE TRIM( BOTH FROM UPPER(c.desc) ) LIKE :cerca ";
		}

		EntityManager em = getEntityManager();
		try {
			javax.persistence.Query q = em
					.createQuery("select c from Broker as c " + cWhere
							+ " order by c.desc");
			
			if (cerca != null && !cerca.isEmpty()) {
				q.setParameter("cerca", '%'+cerca.trim().toUpperCase()+'%' );
			}
			
			List<Broker> ar1 = q.getResultList();
			return ar1;
		} finally {
			em.close();
		}
	}

	public Broker getBrokerxId(int id) {

		EntityManager em = getEntityManager();
		try {
			Broker brokerx = em.find(Broker.class, id);
			return brokerx;
		} finally {
			em.close();
		}
	}

	@SuppressWarnings("finally")
	public Broker addBroker(Broker broker) {
		EntityManager em = getEntityManager();
		try {
			em.getTransaction().begin();
			em.persist(broker);
			em.getTransaction().commit();
			broker = refreshBroker(broker);
		} finally {
			em.close();
			return broker;
		}
	}

	@SuppressWarnings("finally")
	public boolean updateBroker(Broker broker) {

		EntityManager em = getEntityManager();
		try {
			em.getTransaction().begin();
			Broker brokerx = em.find(Broker.class, broker.getId());
			//brokerx.setId(broker.getId());
			brokerx.setAddress(broker.getAddress());
			brokerx.setCity(broker.getCity());
			brokerx.setContact(broker.getContact());
			brokerx.setCountry(broker.getCountry());
			brokerx.setDesc(broker.getDesc());
			brokerx.setEmail(broker.getEmail());
			brokerx.setFax(broker.getFax());
			brokerx.setGsm(broker.getGsm());
			brokerx.setPort(broker.getPort());
			brokerx.setTel(broker.getTel());

			em.getTransaction().commit();
			em.flush();

		} finally {
			em.close();
			return false;
		}
	}

	@SuppressWarnings("finally")
	public Broker refreshBroker(Broker broker) {

		Broker brokerx = broker;
		EntityManager em = getEntityManager();
		try {
			em.getTransaction().begin();
			brokerx = em.find(Broker.class, broker.getId());
			em.flush();
			em.refresh(brokerx);
		} finally {
			em.close();
			return brokerx;
		}
	}

	@SuppressWarnings("finally")
	public boolean removeBroker(Broker broker) {
		EntityManager em = getEntityManager();
		try {
			em.getTransaction().begin();
			Broker brokerx = em.find(Broker.class, broker.getId());
			em.remove(brokerx);
			em.getTransaction().commit();
		} finally {
			em.close();
			return false;
		}
	}

}
